package zuosheng.erfen;

/**
 * @author lj
 * @Description: 在数组中找到一个局部最小的位置
 * @date 2020-11-16 14:11
 * @QQ 851477108
 */


public class GetLessIndex {
    public static void main(String[] args) {
        int[] arr = {7,6,3,1,4,5,6};
        System.out.println(getLessIndex(arr));
        System.out.println(arr[getLessIndex(arr)]);
    }

    public static int getLessIndex(int[] arr){
        int l = arr.length;
        if (arr == null || l == 0){
            return -1;
        }
        if (l == 1 || arr[0] < arr[1]){
            return 0;
        }
        if (arr[l-1] < arr[l-2]){
            return l - 1;
        }

        int left = 1;
        int right = l - 2;
        int mid = 0;
        while (left < right){
            mid = left + (right - left)/2;
            if (arr[mid] > arr[mid-1]){
                right = mid - 1;
            }else if (arr[mid] > arr[mid+1]){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return left;
    }
}
